home *** CD-ROM | disk | FTP | other *** search
- Path: sdrc.com!thor!scjones
- From: larry.jones@sdrc.com (Larry Jones)
- Newsgroups: comp.lang.c
- Subject: Re: #define "creating" strings ?
- Date: 15 Mar 1996 18:10:35 GMT
- Organization: SDRC Engineering Services
- Distribution: world
- Message-ID: <4icbqr$qlt@info1.sdrc.com>
- References: <Do9tsI.H2t@undergrad.math.uwaterloo.ca>
- NNTP-Posting-Host: thor.sdrc.com
- Originator: scjones@thor
-
- In article <Do9tsI.H2t@undergrad.math.uwaterloo.ca>, crpalmer@solo.uwaterloo.ca (Chris Palmer) writes:
- > #define MAKESTRING(x) "x"
- >
- > which does in fact produce strings that have the value of x substituted.
- > [eg: MAKESTRING(foo) gets processed into "foo"].
- >
- > I've verified this on a DEC Alpha (cc) and the GNU C compiler..
- >
- > Is this in fact a supported behaviour that should be portable to most
- > C compilers?
-
- No. This was a bug in one early preprocessor that has been widely
- copied; a careful reading of K&R clearly forbids it, as does the
- ANSI/ISO C Standard (if you'd used gcc -ansi, you would know that). The
- ANSI ``stringizing'' operator is the right way to do this:
-
- #define STRINGIZE(x) #x
- #define MAKESTRING(x) STRINGIZE(x)
-
- #define XYZ 123
-
- STRINGIZE(XYZ); /* "XYZ" */
- MAKESTRING(XYZ); /* "123" */
- ----
- Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070
- larry.jones@sdrc.com
- Shut up and go get me some antiseptic. -- Calvin
-